package com.atguigu.eduservice.controller;

import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.R;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseInfoForm;
import com.atguigu.eduservice.query.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.vo.CoursePublishVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description = "课程管理")
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("/save-course-info")
    public R saveCourseInfo(@ApiParam(name = "EduCourseInfoForm", value = "课程基本信息", required = true)
                            @RequestBody EduCourseInfoForm eduCourseInfoForm) {
        String courseId = eduCourseService.saveCourseInfo(eduCourseInfoForm);
        if (!StringUtils.isEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        } else {
            throw new GuliException(ResultCodeEnum.ADD_ERROR);
        }
    }

    @ApiOperation(value = "根据id查询课程")
    @GetMapping("/course-info/{id}")
    public R getCourseById(@ApiParam(name = "id", value = "课程id", required = true)
                           @PathVariable String id) {
        EduCourseInfoForm eduCourseInfoForm = eduCourseService.getCourseInfoFormById(id);
        return R.ok().data("item", eduCourseInfoForm);
    }

    @ApiOperation(value = "根据id修改课程")
    @PostMapping("/update-course-info/{id}")
    public R updateCourseInfoById(@ApiParam(name = "EduCourseInfoForm", value = "课程基本信息", required = true)
                                  @RequestBody EduCourseInfoForm eduCourseInfoForm,
                                  @ApiParam(name = "id", value = "课程id", required = true)
                                  @PathVariable String id) {
        eduCourseService.updateCourseInfoById(eduCourseInfoForm);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public R getCoursePublishVoById(@ApiParam(name = "id", value = "课程id", required = true)
                                    @PathVariable String id) {
        CoursePublishVo courseInfoForm = eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(@ApiParam(name = "id", value = "课程id", required = true)
                               @PathVariable String id) {
        boolean result = eduCourseService.publishCourseById(id);
        if (!result) {
            return R.error(ResultCodeEnum.PUBLISH_FAIL);
        } else {
            return R.ok();
        }
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{pageSize}")
    public R pageQuery(@ApiParam(name = "page", value = "当前页码", required = true)
                       @PathVariable(value = "page") Long page,
                       @ApiParam(name = "pageSize", value = "每页记录数", required = true)
                       @PathVariable(value = "pageSize") Long pageSize,
                       @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                       CourseQuery courseQuery) {

        Page<EduCourse> pageParam = new Page<>(page, pageSize);
        eduCourseService.pageQuery2(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "根据id删除课程")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "课程id", required = true)
                        @PathVariable String id) {
        boolean result = eduCourseService.removeCourseById(id);
        if (result) {
            return R.ok();
        }else {
            return R.error(ResultCodeEnum.REMOVE_ERROR);
        }
    }

}
